﻿using AutoMapper;
using Comm100.Application.Services;
using Comm100.Framework.Auditing;
using Comm100.Framework.Authorization;
using Comm100.Public.Audit;
using Bot.Application.Dto;
using Bot.Application.Services;
using Bot.Domain.Bo;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using Bot.Domain;
using Comm100.Framework;
using Bot.Domain.Specifications;

namespace Bot.Application.LearningQuestions.Service
{
    public class ChatbotLearningQuestionAppService : BaseAppService, IChatbotLearningQuestionAppService
    {
        private IChatbotLearningQuestionDomainService _domainService;

        public ChatbotLearningQuestionAppService(IChatbotLearningQuestionDomainService domainService) : base()
        {
            this._domainService = domainService;
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        [Audit(BotEntity.LEARNINGQUESTION, AuditAction.DESTROY)]
        public void Delete(Guid id)
        {
            _domainService.Delete(id);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        public Guid BatchAddQuestions(Guid botId, List<string> questions)
        {
            //LearningQuestion learningQuestion = _domainService.Create(Mapper.Map<LearningQuestion>(dto));
            //return Mapper.Map<LearningQuestionDto>(learningQuestion);
            return Guid.NewGuid();
        }

        public ChatbotLearningQuestionDto Get(Guid id, string include="")
        {
            ChatbotLearningQuestion learningQuestion = _domainService.Get(id, include);
            return Mapper.Map<ChatbotLearningQuestionDto>(learningQuestion);
        }

        public PagedListDto<ChatbotLearningQuestionDto> GetList(Guid botId, LearningQuestionQueryDto dto, Paging paging, string include = "")
        {
            ChatbotLearningQuestionFilterSpecification spec = new ChatbotLearningQuestionFilterSpecification(botId, dto.TimeFrom, dto.TimeTo);
            if (string.IsNullOrEmpty(dto.Keyword) && dto.MinScore > 0)
            {
                spec = new ChatbotLearningQuestionFilterSpecification(botId, dto.TimeFrom, dto.TimeTo, dto.MinScore);
            }
            else if (!string.IsNullOrEmpty(dto.Keyword) && dto.MinScore <= 0)
            {
                spec = new ChatbotLearningQuestionFilterSpecification(botId, dto.TimeFrom, dto.TimeTo, dto.Keyword);
            }
            else if (!string.IsNullOrEmpty(dto.Keyword) && dto.MinScore > 0)
            {
                spec = new ChatbotLearningQuestionFilterSpecification(botId, dto.TimeFrom, dto.TimeTo, dto.Keyword, dto.MinScore);
            }
            int count = _domainService.Count(spec);
            spec.ApplyPaging(paging);
            IEnumerable<ChatbotLearningQuestion> list = _domainService.List(spec, include);
            return new PagedListDto<ChatbotLearningQuestionDto>(count, Mapper.Map<IEnumerable<ChatbotLearningQuestionDto>>(list));
        }
    }
}
